import 'package:flutter/material.dart';
import 'package:x3_base_core/x3_base_core.dart';
import 'package:x3_component_core/x3_component_core.dart';
import 'package:x3_form_core/x3_form_core.dart';

class BusinessCustomerDetailsEditBaseTabForm extends StatefulWidget {
  BusinessCustomerDetailsEditBaseTabForm({Key key, this.response, this.dataSource, this.formDataSource}) : super(key: key);

  final BaseHttpResponseModel response;

  final List<dynamic> dataSource;

  final Map<String, dynamic> formDataSource;

  @override
  _BusinessCustomerDetailsEditBaseTabFormState createState() => _BusinessCustomerDetailsEditBaseTabFormState();
}

class _BusinessCustomerDetailsEditBaseTabFormState extends State<BusinessCustomerDetailsEditBaseTabForm> {
  BaseRequestFormProvider baseRequestFormProvider;
  String vipId;

  @override
  void initState() {
    List<BaseHttpRequestModel> _requests = this._initContentRequestFields();
    vipId = BaseUtils.getKeyData(widget.formDataSource, 'vipId').toString();

    baseRequestFormProvider = BaseRequestFormProvider(requests: _requests, dataSource: widget.formDataSource);
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Padding(
          padding: const EdgeInsets.only(top: 12.0, left: 16.0, right: 12.0, bottom: 0),
          child: Text(widget.response.name),
        ),
        BaseCardComponent(
          child: BaseRequestForm(baseRequestFormProvider: baseRequestFormProvider, requestSaveData: _saveFormData),
        ),
      ],
    );
  }

  void _saveFormData(BaseHttpRequestModel request, Map<String, dynamic> data, {bool notShowLoading}) async {
    String service = '/vipbills/vipuserbills/vipclerk/update/base';
    Map<String, dynamic> _dataValue = baseRequestFormProvider.getDataMapKey(request);
    if (notShowLoading == null || !notShowLoading) BaseComponentUtils.showLoadingDialog(context, hint: '正在保存会员信息');
    await dioUtil.fetch(service, params: {'vipId': vipId, ..._dataValue});
    if (notShowLoading == null || !notShowLoading) BaseComponentUtils.hideLoadingDialog(context);
  }

  /// 获取入参
  List<BaseHttpRequestModel> _initContentRequestFields() {
    List<dynamic> items = widget.dataSource ?? [];
    List<BaseHttpRequestModel> requests = [];
    items.forEach((element) {
      String accessMode = BaseUtils.getKeyData(element, 'accessMode').toString();
      String fieldName = BaseUtils.getKeyData(element, 'propertyId').toString();
      String name = BaseUtils.getKeyData(element, 'propertyName').toString();
      String notes = BaseUtils.getKeyData(element, 'propertyTag').toString();
      String inputApi = BaseUtils.getKeyData(element, 'inputApi').toString();
      String inputType = BaseUtils.getKeyData(element, 'inputType').toString();
      String defaultValue = BaseUtils.getKeyData(element, 'inputDefault').toString();
      String propertyRequired = BaseUtils.getKeyData(element, 'propertyRequired').toString();
      BaseHttpRequestModel request = new BaseHttpRequestModel.fromJson({'fieldName': fieldName, 'name': name, 'notes': notes, 'inputApi': inputApi, 'inputType': inputType, 'accessMode': accessMode, 'required': propertyRequired == 'true', 'defaultValue': defaultValue});
      requests.add(request);
    });
    return requests;
  }
}
